package com.jg.view.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.jg.business.delegate.UserDelegate;
import com.jg.dto.UserDTO;
import com.jg.service.IUser;

@Controller("userBean")
@Scope("session")
public class UserBean implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3633700952501041422L;
	
	@Autowired
	private UserDelegate delegate;
	private UserDTO userDto = new UserDTO();
	private List<UserDTO> listaUsuarios = new ArrayList<UserDTO>();
	
	public String pesquisar(){
		IUser userImpl = delegate.get();
		listaUsuarios = userImpl.findAllUsers();
		return null;
	}

	
	public String add(){
		try{
			IUser userImpl = delegate.get();
			userImpl.add(userDto);
//			IUser userImpl = UserDelegate.getInstance().get();
//			userDto = userImpl.get(1L);
			
			
			FacesContext.getCurrentInstance().addMessage("", new FacesMessage("Success!"));	
		}catch (Exception e) {
			FacesMessage message =  new FacesMessage(e.getMessage());
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
			FacesContext.getCurrentInstance().addMessage("", message);
		}
		return null;
	}


	public UserDTO getUser() {
		return userDto;
	}


	public void setUser(UserDTO userDto) {
		this.userDto = userDto;
	}


	public List<UserDTO> getListaUsuarios() {
		return listaUsuarios;
	}


	public void setListaUsuarios(List<UserDTO> listaUsuarios) {
		this.listaUsuarios = listaUsuarios;
	}


	public UserDelegate getDelegate() {
		return delegate;
	}


	public void setDelegate(UserDelegate delegate) {
		this.delegate = delegate;
	}

}
